#ifndef RTU_FILE_DEFINITIONS
#define RTU_FILE_DEFINITIONS

#define SS_NUM  16

#define RTU_FILE
#define FILE_RS

#ifdef FIRST
#define EXTERN
#else
#define EXTERN  extern
#endif


////////////////////////////////////////////////////////////////////////////////////////
#ifdef FILE_RS
typedef struct { 
	 char   *sp;
} FL_FLASH_PARM;

//#define FL_FLASH_NUM   (1+SS_NUM*5+7)
#define FL_FLASH_NUM   254

EXTERN FL_FLASH_PARM   FL_FLASH[FL_FLASH_NUM];

EXTERN unsigned char rtu_set_fno;
EXTERN unsigned char di_fno[SS_NUM];
EXTERN unsigned char do_fno[SS_NUM];
EXTERN unsigned char ai_fno[SS_NUM];
EXTERN unsigned char ci_fno[SS_NUM];
EXTERN unsigned char ao_fno[SS_NUM];
//EXTERN unsigned char sd3_rs_fno,sd3_dim_fno,sd3_dom_fno,sd3_aim_fno,sd3_cim_fno,sd3_aom_fno,sd3_class_fno;

typedef struct { 
	 short  path;
     int    portno;
     char   pnetip[20];
     char   ptcpudp;
	 unsigned char  step;
     unsigned short size;
	 unsigned char  dly;	 
} FL_PARM;
#define FL_NUM   1
EXTERN FL_PARM   FL[FL_NUM];
EXTERN  short file_dcrc,file_fcrc;

EXTERN  unsigned short file_dsize,file_fsize;
EXTERN  unsigned char  file_error;

EXTERN  unsigned short file_dly,file_sec;
EXTERN  char *fl_flash_rptr;
#define MSSize 256
EXTERN  char MSStr[MSSize];
int  getfline(void);

void file_flash_init(void);
//int  update_file_data(int fno,char *mkstr,char *mkdata,char *newdata);

#endif  // FILE_RS

////////////////////////////////////////////////////////////////////////////////////////
#ifdef  RTU_FILE
#define QC_MANU         0
#define QC_MANU_STUS    1
#define QC_UNNOR        2
#define QC_MASK         3
#define QC_SALM         4
#define QC_INVALID      5
#define QC_VINVALID     6
#define QC_RMANU        7

// MD = HC3+SSN4+CNO9 = 16bit
#define MD_HC_MASK    0xE000
#define MD_HC_DI      0x2000  
#define MD_HC_DO      0x4000 
#define MD_HC_AI      0x6000
#define MD_HC_CI      0x8000
#define MD_HC_AO      0xA000

#define MD_SSN_SHIFT     9
#define MD_SSN_MASK   0x0F
#define MD_CNO_SHIFT     0
#define MD_CNO_MASK  0x1FF

////////////////////////////////////
#define NAME_SIZE  16

#define CARD_SS     1
#define METER_SS    9
#define TRX_SS     10

typedef struct { 
    short  DIN,DON,AIN,CIN,AON;
    short  DIS,DOS,AIS,CIS,AOS;
	char   name[NAME_SIZE];
} SS_PARM;
EXTERN SS_PARM  SS[SS_NUM];
EXTERN unsigned short SS_DI_NUM,SS_DO_NUM,SS_AI_NUM,SS_CI_NUM,SS_AO_NUM;


////////////////////////////////////		 
// rmode define 
#define M_NORMAL   0
#define M_SOE      3
#define M_MCD      4

typedef struct { 
    char  name[NAME_SIZE];
    char  rmode;
	char  nul;
} DI_PARM;  // 18
EXTERN DI_PARM *DIP;
EXTERN U8      DI_QCBIT[MAX_DI_NUM];

///////////////////////////////////
typedef struct {
    char name[NAME_SIZE]; 
} DO_PARM;  //16
EXTERN DO_PARM *DOP;

#define ALV_NOR    0
#define ALV_LL     1
#define ALV_HH     4
#define ALV_DIS  0xF
///////////////////////////////////
typedef struct { 
    char     name[NAME_SIZE];
    float    bas;
	float    offset;
	unsigned short LL,HH;
	unsigned char  alv;
	unsigned char  cdly;
} AI_PARM;  // 32
EXTERN AI_PARM *AIP;
EXTERN U8      AI_QCBIT[MAX_AI_NUM];
  
#ifdef  MAX_AO_NUM
typedef struct {
    char name[NAME_SIZE]; 
    float bas;
} AO_PARM;  // 20
EXTERN AO_PARM *AOP;
EXTERN U8      AO_QCBIT[MAX_AO_NUM];
#endif


#ifdef  MAX_CI_NUM 
typedef struct { 
    char name[NAME_SIZE];
    float bas;
} CI_PARM;  // 20
EXTERN CI_PARM *CIP;
EXTERN U8      CI_QCBIT[MAX_CI_NUM];
#endif

#if ((MAX_DI_NUM*18+MAX_DO_NUM*16+MAX_AI_NUM*32+MAX_AO_NUM*20+MAX_CI_NUM*20+20)>SS_MEM_SIZE)
size over SS_MEM_SIZE;
#endif


//////////////////////////////////
#define SRAM_SIZE  2048   

#ifdef SRAM_SIZE 
#define SRAM_SADDR     0xE0084000

#define MSGB_NUM 150
#ifdef MSGB_NUM
typedef struct { 
    int sec;
	int data;
	int var;
} MSGB_PARM;
EXTERN MSGB_PARM  *MSGB;
EXTERN unsigned int *MSGB_h,*MSGB_t;
EXTERN char MSGB_chg;
void MSGB_reset(void);
int  MSGB_rdy(void);
void MSGB_write(int sec,int ms,int ch_md,int stus,int mode,int qcm,int var,int opr);
void MSGB_read(int idx,int *sec,int *ms,int *ch_md,int *stus,int *mode,int *qcm,int *var,int *opr);

// data(32)=>MK(1)+MODE SOE/NOR(1)+QC_MANU(1)+STUS(3)+ms(10)+MD(16)
#define MSGB_MARK       0x80000000

#define MSGB_MODE       0x40000000
#define MSGB_MODE_SOE   1
#define MSGB_MODE_NOR   0

#define MSGB_QCM        0x20000000

#define MSGB_STUS_MASK  0x7
#define MSGB_STUS_SHIFT 26
#define MSGB_STUS_0     0
#define MSGB_STUS_1     1
#define MSGB_STUS_10    2
#define MSGB_STUS_01    3
#define MSGB_STUS_010   4
#define MSGB_STUS_101   5

#define MSGB_STUS_PULSE 5  // DO output
#define MSGB_STUS_TRIP  6  // DO output
#define MSGB_STUS_CLOSE 7  // DO output

#define MSGB_STUS_OPR   7  // AO outpout

#define MSGB_MS_MASK    0x3FF
#define MSGB_MS_SHIFT   16
#define MSGB_MD_MASK    0xFFFF
#define MSGB_MD_SHIFT   0

// var(32) => OPR(4)+VAR(28)
#define MSGB_OPR_SHIFT  28
#define MSGB_OPR_MASK   0xFFFFFFF
#define MSGB_OPR_MAN    1
#define MSGB_OPR_COM    2
#define MSGB_OPR_PLC    3
#endif

#endif  // SRAM_SIZE 

int   getstr(char *s,int *i,char *cstr);
char  *no_to_str(char hc,int ssn,int cno,int ch);
char  *no2str(char hc,int ssn,int cno);
char  *ch2str(char hc,int ch);
int   str2no(char *s,char *hc,int *rno,int *cno);
short ACW_buff(char hc,short ch,int *sv,float *sfv);
short IO_stus(char hc,short ch);
char  *name_str(char hc,int ch);
char  qc_get_bit(char hc,short ch,char qcbit);
void  qc_set_bit(char hc,short ch,char qcbit,char stus);
char  qc_menu_code(char hc,short ch);

char  md_hc_code(unsigned short md_hc);
short get_ch(short hc,short ssn,short cno);
short get_ssn_sno(short hc,short ch,short *ssn,short *cno);
short md_data(unsigned short md,char *hc,short *ssn,short *cno);
unsigned short md_value(char hc,short ssn,short cno);
unsigned short md_value_ch(char hc,int ch);
void init_mem(void);

#endif   //RTU_FILE
////////////////////////////////////////////////////////////////////////////////////// 
#endif   //RTU_FILE_DEFINITIONS
